from flask import Flask
from settings import map_config


# 通过父类我们去获取配置信息中的测试环境
# 创建app
def creat_app(config_type):
    app = Flask(__name__)
    # 加载项目的配置信息
    app.config.from_object(map_config.get(config_type))

    # 加载日志处理的工具
    from comment.utils.logging import create_logging
    create_logging(app)

    # 初始化限制器
    from comment.utils.limiter import limiter as lmt
    lmt.init_app(app)

    # 初始化SQL alchemy
    from comment.model import db
    # 初始换app
    db.init_app(app)

    # 加载user模块的蓝图
    from Shopping.resources.user import user_bp
    # 用户模块的蓝图注册到app中去
    app.register_blueprint(user_bp)

    # 加载index商品下面的蓝图
    from Shopping.resources.index import index_bp
    app.register_blueprint(index_bp)

    # 加载模块的蓝图
    from Shopping.resources.goods import goods_bp
    from Shopping.resources.coupon import coupon_bp
    from Shopping.resources.cart import cart_bp
    from Shopping.resources.address import address_bp
    from Shopping.resources.order import order_bp
    app.register_blueprint(goods_bp)
    app.register_blueprint(coupon_bp)
    app.register_blueprint(cart_bp)
    app.register_blueprint(address_bp)
    app.register_blueprint(order_bp)

    # 添加全局请求钩子
    from comment.utils.requests_wares import jwt_request_authorization
    # before_request 这个指的是在请求之前
    app.before_request(jwt_request_authorization)  # 所有服务器的请求都在当前请求钩子中生效

    # 初始化redis数据库，用来保存验证码
    from comment.utils.shopping_redis import redis_client
    redis_client.init_app(app)

    # 返回app
    return app
